<template>
  <!--基本信息-->
  <view>
    <myHead
      :title="title"
      :right="true"
      text_right="保存"
      @clickRight="clickRight"
    ></myHead>
    <view class="conten">
      <view class="li-box fl-lr-c" @click="uploadAvatar">
        <view class="name">头像(选填)</view>
        <view>
          <image
            v-if="headImg"
            class="logo"
            :src="URL + headImg"
            mode=""
          ></image>
          <view v-else class="fl-t">
            <view class="content"> 请上传头像 </view>
            <u-icon name="arrow-right"></u-icon>
          </view>
        </view>
      </view>
      <view class="li-box fl-lr-c">
        <view class="name">姓名</view>
        <input
          placeholder-class="input_pal"
          type="text"
          v-model="name"
          :disabled="type == 2 && Status == 1"
          placeholder="请输入"
        />
      </view>

      <view class="li-box fl-lr-c">
        <view class="name">性别</view>
        <view class="fl-t">
          <u-radio-group v-model="sex" placement="row">
            <u-radio
              :customStyle="{ marginLeft: '25px' }"
              v-for="(item, index) in sexList"
              :key="index"
              :label="item.name"
              :name="item.value"
              @change="radioChange1"
            >
            </u-radio>
          </u-radio-group>
        </view>
      </view>

      <view class="li-box fl-lr-c">
        <view class="name">当前身份</view>
        <view class="fl-t">
          <u-radio-group v-model="identity" placement="row">
            <u-radio
              :customStyle="{ marginLeft: '25px' }"
              v-for="(item, index) in identityList"
              :key="index"
              :label="item.name"
              :name="item.value"
              @change="radioChange2"
            >
            </u-radio>
          </u-radio-group>
        </view>
      </view>
      <u-datetime-picker
        v-if="show1"
        :show="show1"
        mode="date"
        closeOnClickOverlay
        v-model="value2"
        @close="show1 = false"
        @cancel="show1 = false"
        :minDate="birthMinTime"
        :maxDate="birthMaxTime"
        @confirm="confirm1"
      >
      </u-datetime-picker>
      <view class="li-box fl-lr-c" @click="chushengTime">
        <view class="name">出生年月</view>
        <view class="fl-t">
          <view class="content">
            <text>{{ birth ? birth : "请选择" }}</text>
          </view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
      <u-datetime-picker
        v-if="show2"
        :show="show2"
        mode="year-month"
        v-model="value1"
        closeOnClickOverlay
        @close="show2 = false"
        @cancel="show2 = false"
        :minDate="workMinTime"
        :maxDate="birthMaxTime"
        @confirm="confirm2"
      >
      </u-datetime-picker>
      <view class="li-box fl-lr-c" @click="showTime2">
        <view class="name">{{
          this.identity ? "毕业时间" : "参加工作时间"
        }}</view>
        <view class="fl-t">
          <view class="content">
            <text v-if="work">{{ work | formatDateYM }}</text>
            <text v-else>请选择</text>
          </view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
      <u-picker
        :show="showJobState"
        :defaultIndex="defaultIndex"
        closeOnClickOverlay
        @cancel="showJobState = false"
        @close="showJobState = false"
        keyName="text"
        :columns="jobStateList"
        @confirm="jobchange"
      >
      </u-picker>
      <view class="li-box fl-lr-c" @click="gongzuoTime">
        <view class="name">当前求职状态</view>
        <view class="fl-t">
          <view class="content">
            {{ joblsit2text || "请选择" }}
          </view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
      <view class="li-box fl-lr-c" @click="shoujihao">
        <view class="name">手机号</view>
        <view style="width: 85%">
          <input
            placeholder-class="input_pal"
            type="number"
            disabled
            v-model="phone"
            placeholder="请输入"
          />
        </view>
        <u-icon name="arrow-right"></u-icon>
      </view>
      <view class="li-box fl-lr-c">
        <view class="name">微信</view>
        <view>
          <input
            :adjust-position="false"
            placeholder-class="input_pal"
            type="text"
            v-model="wx"
            placeholder="请输入"
          />
        </view>
      </view>
      <view class="li-box fl-lr-c" @click="youxiang">
        <view class="name">邮箱（选填）</view>
        <view style="width: 80%">
          <input
            placeholder-class="input_pal"
            :adjust-position="false"
            type="text"
            v-model="mailbox"
            placeholder="请输入"
            style="text-align: end"
            disabled="true"
          />
        </view>
        <u-icon name="arrow-right"></u-icon>
      </view>
      <!-- 	<view class="li-box fl-lr-c">
				<view class="name">居住地址</view>
				<view >
					<view class="content" style="text-align: start; margin-left: 20rpx">
						{{ Address || "请选择" }}
					</view>
					<input placeholder-class="input_pal" :adjust-position="false" type="text" v-model="Address"
						placeholder="请选择"  />
				</view>
				<u-icon name="arrow-right" @click="xxAddress"></u-icon>
			</view> -->
      <view class="li-box fl-lr-c" @click="xxAddress">
        <view class="name">居住地址</view>
        <view style="width: 80%">
          <input
            placeholder-class="input_pal"
            :adjust-position="false"
            type="text"
            v-model="Address"
            placeholder="请选择"
            style="text-align: end"
          />
        </view>
        <u-icon name="arrow-right"></u-icon>
      </view>
    </view>
    <addPopup
      :show.sync="addShow"
      v-if="addShow"
      :addTitle="addTitle"
      :addxxAddressItem="addxxAddressItem"
      @editAddress="editAddress"
    ></addPopup>
  </view>
</template>

<script>
import myHead from "@/pages/component/baseHead.vue";
import bottomBut from "@/pages/component/bottomBut.vue";
import pickRegions from "@/pages/component/pick-regions/pick-regions.vue";
import utils from "@/common/tui-utils/tui-utils.js";
import addPopup from "@/pages/component/addPopup/index.vue";
import {
  atPresentTime,
  atPresentTimeX,
  specifyAfterVX,
  TimeString,
} from "@/utils/Time";
export default {
  data() {
    return {
      Status: 0,
      value1: "",
      value2: "",
      URL: this.$config.domain.staticUrl,
      type: 0, //1add  2编辑
      title: "基本信息",
      show2: false,
      show1: false,
      showJobState: false,
      jobStateList: [],
      identityList: [
        {
          name: "学生",
          value: 1,
        },
        {
          name: "职场人",
          value: 0,
        },
      ],
      sexList: [
        {
          name: "男",
          value: 1,
        },
        {
          name: "女",
          value: 2,
        },
      ],
      headImg: "", //图片访问地址
      f_cunchulujing: "", //存数据库的地址  如果空就不提交
      f_fangwenlujing: "",
      name: "", //姓名
      sex: 0, //性别
      identity: 0, //身份
      birth: 0, //出生年份
      work: "", //工作时间
      jobState: "", //求职状态
      wx: "",
      mailbox: uni.getStorageSync("homelist").UserInfo.Email || "",
      phone: uni.getStorageSync("homelist").UserInfo.PhoneNumber,
      PcaAddress: "",
      Address: "",
      Longitude: 0,
      Latitude: 0,
      selRegion: "", //选择的省市区
      filter: ["重庆市", "上海市", "天津市", "北京市"],
      defaultRegion: ["", "", ""],
      city: "",
      isSel: false, //是否是选择
      // 当前id
      Id: 0,
      // 父id
      fuid: 0,
      resumeList: {},
      //#region 出生时间
      birthMinTime: 0,
      birthMaxTime: 0,
      //#endregion
      addShow: false, //控制子组件显示隐藏
      addTitle: "居住地址",
      addxxAddressItem: {},
    };
  },
  //子组件
  components: {
    myHead,
    bottomBut,
    pickRegions,
    addPopup,
  },
  computed: {
    joblsit2text() {
      let arr = [];
      if (this.jobStateList.length > 0) {
        arr = this.jobStateList[0].filter((res) => {
          return this.jobState == res.value;
        });
      }
      if (arr.length == 0) {
        return "";
      } else {
        return arr[0].text || " ";
      }
    },
    defaultIndex() {
      let arr = [];
      arr.push(Number(this.jobState));
      return arr;
    },
    //工作时间最小值
    workMinTime() {
      if (this.birth) {
        let time = specifyAfterVX(this.birth, 10); //传入时间的后十年
        return this.birthMaxTime > time ? time : this.birthMaxTime;
      } else {
        return this.birthMinTime;
      }
    },
  },
  onShow() {
    //#region 计算出生年月的最大最小值
    this.birthMaxTime = atPresentTime();
    this.birthMinTime = atPresentTimeX(60);
    //#endregion

    this.$util.currentUser().then((res) => {
      this.name = uni.getStorageSync("homelist").UserInfo.RealName;
      this.mailbox = uni.getStorageSync("homelist").UserInfo.Email;
      this.phone = uni.getStorageSync("homelist").UserInfo.PhoneNumber;
    });
  },
  onLoad(o) {
    const app = getApp();
    this.Status = app.globalData.user.Status;
    this.mailbox = app.globalData.user.Email;
    this.phone = app.globalData.user.PhoneNumber;
    let nowDate = new Date();
    this.year = nowDate.getFullYear();
    this.type = o.type;
    this.fuid = o.fuid;
    this.$http.huirc
      .resumemy()
      .then((res) => {
        if (res.Success) {
          res = res.Result;
          //处理数据
          this.resumeList = res.BaseInfo; //基本信息
        }
      })
      .then((res1) => {
        if (this.type == 2) {
          this.headImg = this.resumeList.Avatar;
          if (uni.getStorageSync("homelist").UserInfo.RealName) {
            this.name = uni.getStorageSync("homelist").UserInfo.RealName;
          } else {
            this.name = this.resumeList.Name;
          }
          this.sex = this.resumeList.Sex;
          this.birth = this.resumeList.Birthday.split(" ")[0];
          this.value2 = new Date(this.birth.replace(/-/g, "/")).getTime();
          this.work = this.resumeList.StartWorkDate.split(" ")[0];
          this.value1 = new Date(this.work.replace(/-/g, "/")).getTime();
          this.jobState = this.resumeList.CurrentStatus;
          this.identity = this.resumeList.CurrentRole;
          this.wx = this.resumeList.WeixinNo;
          // this.mailbox = this.resumeList.Email;
          this.Address = this.resumeList.Address;
          this.Longitude = this.resumeList.Longitude;
          this.Latitude = this.resumeList.Latitude;

          this.f_cunchulujing = this.resumeList.f_cunchulujing;
          this.f_fangwenlujing = this.resumeList.f_fangwenlujing;
          this.f_id = o.f_id;
        }
        if (!this.Longitude && !this.Latitude) {
          let tabt = this;
          uni.getLocation({
            type: "wgs84",
            success: function (res) {
              tabt.Longitude = Math.abs(res.longitude);
              tabt.Latitude = Math.abs(res.latitude);
            },
          });
        }
      });
  },
  watch: {
    identity: {
      handler(val) {
        if (val == 1) {
          this.xialadata(423348254777477);
        } else if (val == 0) {
          this.xialadata(390788264784005);
        }
      },
      immediate: true,
      deep: true,
    },
  },
  // 计算方法
  methods: {
    //关闭地址弹窗
    editAddress(item) {
      this.addShow = false;
      this.Address = item.Address;
    },
    xxAddress() {
      // this.xxAddress();
      this.addShow = true;
      let that = this;
      this.addxxAddressItem = {
        Latitude: that.Latitude, //经度
        Longitude: that.Longitude, //纬度
        Address: that.Address, //详细地址
        PcaAddress: "", //区 街道 地址
        //省市区code值
        AreaCode: "",
        CityCode: "",
        ProvinceCode: "",
      };
    },
    chushengTime() {
      if (!this.birth) {
        this.value2 = TimeString(this.birthMaxTime);
      } else {
        this.value2 = this.birth;
      }
      this.show1 = true;
    },
    gongzuoTime() {
      if (!this.work) {
        this.value1 = TimeString(this.birthMaxTime);
      } else {
        this.value2 = this.work;
      }
      this.showJobState = true;
    },
    xialadata(val) {
      this.$http.dic.DataSelector(val).then((res) => {
        if (res.Success) {
          this.jobStateList = this.xialist(res.Result);
        }
      });
    },
    xialist(arr) {
      let drr = [];
      arr.forEach((res) => {
        drr.push({
          text: res.FullName,
          value: res.EnCode,
        });
      });
      return [drr];
    },
    // xxAddress() {
    // 	let that = this;
    // 	uni.chooseLocation({
    // 		latitude: Number(that.Latitude),
    // 		longitude: Number(that.Longitude),
    // 		success: function(res) {
    // 			//经度
    // 			that.Longitude = res.longitude;
    // 			//纬度
    // 			that.Latitude = res.latitude;
    // 			that.Address = res.address;
    // 		},
    // 		fail: function(e) {},
    // 	});
    // },
    // 绑定邮箱
    youxiang() {
      uni.navigateTo({
        url: "/pages/views/packageB/type-jobhunting/jibenshezhi/jibenziliao/xiugaiyouxiang",
      });
    },
    shoujihao() {
      uni.navigateTo({
        url: "/pages/views/packageB/type-jobhunting/jibenshezhi/jibenziliao/xiugaishojihao",
      });
    },
    //上传头像
    uploadAvatar() {
      var that = this;
      uni.chooseImage({
        count: 1,
        sizeType: ["original", "compressed"],
        success: (res) => {
          const tempFilePaths = res.tempFilePaths; //图片临时数组
          that.$util
            .AuploadFile({
              src: tempFilePaths[0],
            })
            .then((res) => {
              if (res.Success) {
                that.headImg = res.Result.Path;
              }
            });
        },
      });
    },

    clickRight() {
      //校验
      if (!utils.trim(this.name).length) {
        this.$util.toast("请输入姓名");
        return;
      }
      if (!this.sex) {
        this.$util.toast("请选择性别");
        return;
      }
      if (this.birth == 0) {
        this.$util.toast("请选择出生年月");
        return;
      }

      if (!this.work) {
        this.$util.toast("请选择参加工作时间");
        return;
      }
      if (this.jobState === "") {
        this.$util.toast("请选择求职状态");
        return;
      }
      if (this.wx == "") {
        this.$util.toast("请输入微信号");
        return;
      } else {
        let mywx = /^[a-zA-Z][a-zA-Z0-9_-]{5,19}$/;
        let myphone = /^[1][3,4,5,6.7,8,9][0-9]{9}$/;
        if (!myphone.test(this.wx) && !mywx.test(this.wx)) {
          this.$util.toast("微信号格式不对");
          return;
        }
      }
      // if (this.mailbox.length) {
      // 	var myReg = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
      // 	if (!myReg.test(this.mailbox)) {
      // 		this.$util.toast("邮箱格式不对!");
      // 		return;
      // 	}
      // }
      if (!utils.trim(this.Address).length) {
        this.$util.toast("请选择居住地址");
        return;
      }
      if (this.type == 2) {
        //编辑修改
        this.$http.huirc
          .hrcresumeup({
            Id: this.fuid,
            Name: this.name,
            CurrentStatus: this.jobState,
            Sex: this.sex,
            WeixinNo: this.wx,
            Birthday: this.birth,
            Email: this.mailbox,
            Avatar: this.headImg,
            StartWorkDate: this.work,
            PcaAddress: this.PcaAddress,
            Address: this.Address,
            Longitude: this.Longitude,
            Latitude: this.Latitude,
            CurrentRole: this.identity,
          })
          .then((res) => {
            if (res.Success) {
              this.$util.toast("保存成功");
              setTimeout(() => {
                uni.navigateBack();
              }, 1000);
            }
          });
      } else {
        this.$http.huirc
          .hrcresumecr({
            Name: this.name,
            CurrentStatus: this.jobState,
            Sex: this.sex,
            WeixinNo: this.wx,
            Birthday: this.birth,
            Email: this.mailbox,
            Avatar: this.headImg,
            StartWorkDate: this.work,
            PcaAddress: this.PcaAddress,
            Address: this.Address,
            Longitude: this.Longitude,
            Latitude: this.Latitude,
            CurrentRole: this.identity,
          })
          .then((res) => {
            if (res.Success) {
              this.$util.toast("保存成功");
              setTimeout(() => {
                uni.navigateBack();
              }, 1000);
            }
          });
      }
    },
    jobchange(e) {
      this.jobState = e.value[0].value;
      this.showJobState = false;
    },

    confirm1(e) {
      const timeFormat = uni.$u.timeFormat;
      let ds = timeFormat(e.value, "yyyy-mm-dd");
      this.birth = ds;
      this.work = "";
      this.show1 = false;
    },
    showTime2() {
      this.show2 = true;
    },
    confirm2(e) {
      const timeFormat = uni.$u.timeFormat;
      let ds = timeFormat(e.value, "yyyy-mm-dd");
      if (this.birth > ds) {
        this.$util.toast("工作时间小于出生日期");
        this.show2 = false;
        return;
      }

      this.work = ds;
      this.show2 = false;
    },
    radioChange1(e) {
      this.sex = e;
    },
    radioChange2(e) {
      this.identity = e;
    },
  },
};
</script>

<style lang="scss" scoped>
/deep/ .input-placeholder {
  color: #999999 !important;
}

.li-box {
  background-color: #fff;

  .name {
    font-size: 30rpx;
    font-family: PingFang SC;
    font-weight: bold;
    color: #000000;
    flex-shrink: 0;
  }

  .logo {
    width: 106rpx;
    height: 106rpx;
    border-radius: 50%;
  }

  .input_pal,
  input,
  .content {
    font-size: 30rpx;
    font-family: PingFang SC;
    font-weight: 500;
    color: #999999;
    text-align: end;
  }

  .radio-box {
    margin-left: 50rpx;

    .radio-text {
      font-size: 30rpx;
      font-family: PingFang SC;
      font-weight: 500;
      color: #999999;
      padding-left: 20rpx;
    }
  }
}
</style>